<?php
	/**
	* 
	* Add request to calendar
	* @author trungdt 
	* @since Nov 3, 2011
	* 
	* Change logs
	* 
	*/
	if (!defined("EXPONENT")) exit("");
		
	$item = null;
	if (isset($_POST['id'])) {
		$item = $db->selectObject("requestmodule_request","id=".intval($_POST['id']));
		$loc = unserialize($item->location_data);
		if (exponent_permissions_check(array("administrate", "add_calendar"), $loc) && ($item->approve == 2) && !($item->event_id > 0))
		{
			$config = $db->selectObject('requestmodule_config',"location_data='".serialize($loc)."'");
			
			// build loc for calendar source
			$cloc=null;
			$cloc->mod="calendarmodule";
			$cloc->src=$config->calendar_src;
			$cloc->int="";
			$cloc=serialize($cloc);
			
			// build new calendar object
			$event=null;
			$event->location_data = $cloc;
			$event->title=$item->fullname;
			$event->body=requestmodule::_BuildBody_for_calendar($item);
			$event->eventstart=$item->from_date;
			$event->eventend=$item->from_date+$item->time_length*60;
			$event->posted=aboid_getCurrentTime();
			$event->poster=$user->id;
			$event->approve=2;
			$event->approver="{$user->firstname} {$user->lastname} ({$user->username})";
			$event->approve_date=aboid_getCurrentTime();
			$event->repeat_type=-1;
			$event->eventstart_day=date("j", $event->eventstart);
			$event->eventstart_month=date("n", $event->eventstart);
			
			$event->category_id=$item->cat_id;
			
			// add
			$item->event_id=$db->insertObject($event,"calendar");
			
			$db->updateObject($item,"requestmodule_request");
		}
		echo "OK";
	}
	exponent_theme_setFlagAjaxAction(true);
?>
